package com.jinxin.platform.cddatadict.rpc;

import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.api.annotation.*;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.annotation.RegBusiness;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cddatadict.pojo.domains.DataDictItem;
import com.jinxin.platform.cddatadict.pojo.domains.DataDictType;
import com.jinxin.platform.cddatadict.pojo.entity.Constant;
import com.jinxin.platform.cddatadict.pojo.entity.IdNameEntity;
import com.jinxin.platform.cddatadict.service.SysDictItemService;
import com.jinxin.platform.cddatadict.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-07-03
 * @Author: Lingnan
 **/
@RegBusiness(moduleKey = Constant.MODULE_KEY, className = Constant.MODULE_CLASS_NAME)
@Service
public class DictRpcService {

    @Autowired
    private SysDictTypeService dictTypeService;
    @Autowired
    private SysDictItemService dictItemService;

    @APCOSApiOperation(value = "selectDictitemsByType",description = "根据字典类型查询字典项",infDict = "根据字典类型查询字典项",
            reqDemo = "{\"dictType\": \"业务类型\"}",
            respDemo = "{\n" +
                    "    \"data\":[\n" +
                    "        {\n" +
                    "            \"id\":\"0773fa8a0b4944c1bc72682f8e7ef0c7\",\n" +
                    "            \"dictType\":\"481d7d1144dc457f9436ea89f06e7e89\",\n" +
                    "            \"dictName\":\"记录表\",\n" +
                    "            \"dictKey\":\"记录表\",\n" +
                    "            \"dictReadme\":\"记录表\",\n" +
                    "            \"createTime\":\"2023-05-15 15:39:57\",\n" +
                    "            \"updateTime\":null,\n" +
                    "            \"dictLevel\":1,\n" +
                    "            \"itemList\":[\n" +
                    "                {\n" +
                    "                    \"id\":\"0197c76c182d45039881ac4fe63b1a23\",\n" +
                    "                    \"dictType\":\"0773fa8a0b4944c1bc72682f8e7ef0c7\",\n" +
                    "                    \"dictName\":\"1号记录表\",\n" +
                    "                    \"dictKey\":\"1号记录表ID\",\n" +
                    "                    \"dictReadme\":\"1号记录表\",\n" +
                    "                    \"createTime\":\"2023-05-15 15:41:11\",\n" +
                    "                    \"updateTime\":null,\n" +
                    "                    \"dictLevel\":2,\n" +
                    "                    \"itemList\":[\n" +
                    "                        {\n" +
                    "                            \"id\":\"1705051715665174528\",\n" +
                    "                            \"dictType\":\"0197c76c182d45039881ac4fe63b1a23\",\n" +
                    "                            \"dictName\":\"111\",\n" +
                    "                            \"dictKey\":\"222\",\n" +
                    "                            \"dictReadme\":\"111\",\n" +
                    "                            \"createTime\":\"2023-09-22 10:49:42\",\n" +
                    "                            \"updateTime\":null,\n" +
                    "                            \"dictLevel\":3\n" +
                    "                        }\n" +
                    "                    ]\n" +
                    "                }\n" +
                    "            ]\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}",
            infType = APCOSApiConstans.INF_TYPE_OTHER,infCategory = APCOSApiConstans.INF_CATEGORY_ALL,
            developers = {"那个男人"})
    @APCOSApiRequests({
            @APCOSApiRequest(name = "dictType", value = "字典类型", dict = "字典类型",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,required = true,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典类型"),
    })
    @APCOSApiResponses({
//            @APCOSApiResponse(name = "status", value = "状态码", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
//                    dataType = APCOSApiConstans.DATA_TYPE_NUM, lenth = "38,0",dict = "状态码",remark = "状态码",keyType = APCOSApiConstans.KEY_TYPE_STR),
//            @APCOSApiResponse(name = "msg", value = "提示信息", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
//                    dataType = APCOSApiConstans.DATA_TYPE_STR, lenth = "100",dict = "提示信息",remark = "提示信息",keyType = APCOSApiConstans.KEY_TYPE_STR),
//            @APCOSApiResponse(name = "success", value = "成功与否", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
//                    dataType = APCOSApiConstans.DATA_TYPE_STR, lenth = "10",dict = "成功与否",remark = "成功与否",keyType = APCOSApiConstans.KEY_TYPE_STR),
            @APCOSApiResponse(name = "data", value = "返回的数据列表", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_ARR,dict = "返回的数据列表",remark = "返回的数据",keyType = APCOSApiConstans.KEY_TYPE_LIST),
            @APCOSApiResponse(name = "data.id", value = "字典项id", dict = "字典项id",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典项id"),
            @APCOSApiResponse(name = "data.dictType", value = "字典类别", dict = "字典类别",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典类别"),
            @APCOSApiResponse(name = "data.dictName", value = "字典名称", dict = "字典名称",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典名称"),
            @APCOSApiResponse(name = "data.dictKey", value = "字典键值", dict = "字典键值",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典键值"),
            @APCOSApiResponse(name = "data.dictReadme", value = "字典说明", dict = "字典说明",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "255",remark = "字典说明"),
            @APCOSApiResponse(name = "data.createTime", value = "创建时间", dict = "创建时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "创建时间"),
            @APCOSApiResponse(name = "data.updateTime", value = "更新时间", dict = "更新时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "更新时间"),
            @APCOSApiResponse(name = "data.dictLevel", value = "字典层级", dict = "字典层级",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "10",remark = "字典层级"),
            @APCOSApiResponse(name = "data.itemList", value = "二级字典项列表", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_ARR,dict = "二级字典项列表",remark = "二级字典项列表",keyType = APCOSApiConstans.KEY_TYPE_LIST),
            @APCOSApiResponse(name = "data.itemList.id", value = "二级字典项id", dict = "二级字典项id",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "二级字典项id"),
            @APCOSApiResponse(name = "data.itemList.dictType", value = "二级字典类别", dict = "二级字典类别",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "二级字典类别"),
            @APCOSApiResponse(name = "data.itemList.dictName", value = "二级字典名称", dict = "二级字典名称",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "二级字典名称"),
            @APCOSApiResponse(name = "data.itemList.dictKey", value = "二级字典键值", dict = "二级字典键值",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典键值"),
            @APCOSApiResponse(name = "data.itemList.dictReadme", value = "二级字典说明", dict = "二级字典说明",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "255",remark = "二级字典说明"),
            @APCOSApiResponse(name = "data.itemList.createTime", value = "二级创建时间", dict = "二级创建时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "二级创建时间"),
            @APCOSApiResponse(name = "data.itemList.updateTime", value = "二级更新时间", dict = "二级更新时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "二级更新时间"),
            @APCOSApiResponse(name = "data.itemList.dictLevel", value = "二级字典层级", dict = "二级字典层级",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "10",remark = "二级字典层级"),
            @APCOSApiResponse(name = "data.itemList.itemList", value = "三级字典项列表", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_ARR,dict = "三级字典项列表",remark = "三级字典项列表",keyType = APCOSApiConstans.KEY_TYPE_LIST),
            @APCOSApiResponse(name = "data.itemList.itemList.id", value = "三级字典项id", dict = "三级字典项id",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "三级字典项id"),
            @APCOSApiResponse(name = "data.itemList.itemList.dictType", value = "三级字典类别", dict = "三级字典类别",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "三级字典类别"),
            @APCOSApiResponse(name = "data.itemList.itemList.dictName", value = "三级字典名称", dict = "三级字典名称",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "三级字典名称"),
            @APCOSApiResponse(name = "data.itemList.itemList.dictKey", value = "三级字典键值", dict = "三级字典键值",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "三级字典键值"),
            @APCOSApiResponse(name = "data.itemList.itemList.dictReadme", value = "三级字典说明", dict = "三级字典说明",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "255",remark = "三级字典说明"),
            @APCOSApiResponse(name = "data.itemList.itemList.createTime", value = "三级创建时间", dict = "三级创建时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "三级创建时间"),
            @APCOSApiResponse(name = "data.itemList.itemList.updateTime", value = "三级更新时间", dict = "三级更新时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "三级更新时间"),
            @APCOSApiResponse(name = "data.itemList.itemList.dictLevel", value = "三级字典层级", dict = "三级字典层级",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "10",remark = "三级字典层级"),
    })
    public JsonResult selectDictitemsByType(Map param1){
        String dictType = (String)param1.get("dictType");
        if(param1 == null || StringUtils.isEmpty(dictType)){
            return new JsonResult(JsonResult.CODE_404,"数据字典项类型不能为空",null);
        }
        HashMap param = new HashMap();
        param.put("typeName", dictType);
        DataDictType dataDictType = dictTypeService.selectOne(param);
        if(dataDictType != null){
            String typeId = dataDictType.getId();
            if(!StringUtils.isEmpty(typeId)){
                HashMap param2 = new HashMap();
                param2.put("dictType", typeId);
                List<DataDictItem> list = dictItemService.list(param2);
                List<DataDictItem> listNew = new ArrayList<>();
                list.forEach(item->{
                    HashMap param3 = new HashMap();
                    param3.put("dictType", item.getId());
                    List<DataDictItem> list2 = dictItemService.list(param3);
                    List<DataDictItem> listNew2 = new ArrayList<>();
                    list2.forEach(item2->{
                        HashMap param4 = new HashMap();
                        param4.put("dictType", item2.getId());
                        item2.setItemList(dictItemService.list(param4));
                        listNew2.add(item2);
                    });
                    item.setItemList(listNew2);
                    listNew.add(item);
                });
                JSONObject dataJson = new JSONObject();
                dataJson.put("data",listNew);
                return new JsonResult(JsonResult.CODE_200,"获取字典项列表成功",dataJson);
            }
            return new JsonResult(JsonResult.CODE_404,"字典类型id为空",null);

        }else {
            return new JsonResult(JsonResult.CODE_404,"未找到对应类型的字典项",null);
        }

    }

    @APCOSApiOperation(value = "selectTypeList",description = "查询字典类型列表",infDict = "查询字典类型列表",
            reqDemo = "{}",
            respDemo = "{" +
                    "\"data\":[{\"id\":\"字典类型id\",\"typeName\":\"字典类型名称\",\"typeReadme\":\"字典类型说明\"," +
                    "\"createTime\":\"创建时间\", \"updateTime\":\"更新时间\"}]}",
            infType = APCOSApiConstans.INF_TYPE_OTHER,infCategory = APCOSApiConstans.INF_CATEGORY_ALL,
            developers = {"那个男人"})
    @APCOSApiRequests({
    })
    @APCOSApiResponses({
//            @APCOSApiResponse(name = "status", value = "状态码", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
//                    dataType = APCOSApiConstans.DATA_TYPE_NUM, lenth = "38,0",dict = "状态码",remark = "状态码",keyType = APCOSApiConstans.KEY_TYPE_STR),
//            @APCOSApiResponse(name = "msg", value = "提示信息", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
//                    dataType = APCOSApiConstans.DATA_TYPE_STR, lenth = "100",dict = "提示信息",remark = "提示信息",keyType = APCOSApiConstans.KEY_TYPE_STR),
//            @APCOSApiResponse(name = "success", value = "成功与否", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
//                    dataType = APCOSApiConstans.DATA_TYPE_STR, lenth = "10",dict = "成功与否",remark = "成功与否",keyType = APCOSApiConstans.KEY_TYPE_STR),
            @APCOSApiResponse(name = "data", value = "返回的数据列表", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_ARR,dict = "返回的数据列表",remark = "返回的数据",keyType = APCOSApiConstans.KEY_TYPE_LIST),
            @APCOSApiResponse(name = "data.id", value = "字典类型id", dict = "字典类型id",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "32",remark = "字典类型id"),
            @APCOSApiResponse(name = "data.typeName", value = "字典类型名称", dict = "字典类型名称",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典类型名称"),
            @APCOSApiResponse(name = "data.typeReadme", value = "字典类型说明", dict = "字典类型说明",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "255",remark = "字典类型说明"),
            @APCOSApiResponse(name = "data.createTime", value = "添加时间", dict = "添加时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "添加时间"),
            @APCOSApiResponse(name = "data.updateTime", value = "修改时间", dict = "修改时间",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "255",remark = "修改时间"),
    })
    public JsonResult selectTypeList(Map param1){
        List<DataDictType> list = dictTypeService.list(new HashMap());
        JSONObject dataJson = new JSONObject();
        dataJson.put("data",list);
        return JsonResult.ok("success",dataJson);
    }

    @APCOSApiOperation(value = "selectParentByChild",description = "根据子级id查询父级字典",infDict = "根据子级id查询父级字典",
            reqDemo = "{\"childId\": \"子级id\"}",
            respDemo = "{\"data\":{\"id\":\"字典类型id\",\"name\":\"字典名称\"}}",
            infType = APCOSApiConstans.INF_TYPE_OTHER,infCategory = APCOSApiConstans.INF_CATEGORY_ALL,
            developers = {"那个男人"})
    @APCOSApiRequests({
            @APCOSApiRequest(name = "childId", value = "子级id", dict = "子级id",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,required = true,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "100",remark = "子级id"),
    })
    @APCOSApiResponses({
            @APCOSApiResponse(name = "data", value = "返回的数据对象", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_OBJ,dict = "返回的数据对象",remark = "返回的数据",keyType = APCOSApiConstans.KEY_TYPE_OBJ),
            @APCOSApiResponse(name = "data.id", value = "字典类型id", dict = "字典类型id",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典类型id"),
            @APCOSApiResponse(name = "data.name", value = "字典名称", dict = "字典名称",
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "100",remark = "字典名称"),
    })
    public JsonResult selectParentByChild(Map param1){
        String childId = (String)param1.get("childId");
        if(StringUtils.isEmpty(childId)){
            return JsonResult.error("子级id不为空");
        }
        DataDictItem dataDictItem = dictItemService.selectByKey(childId);
        if(dataDictItem == null){
            return JsonResult.error("字典项不存在");
        }
        String dictType = dataDictItem.getDictType();
        IdNameEntity entity = new IdNameEntity();
        JSONObject dataJson = new JSONObject();
        DataDictItem dataDictItem1 = dictItemService.selectByKey(dictType);
        if(dataDictItem1 != null){
            entity.setId(dataDictItem1.getId());
            entity.setName(dataDictItem1.getDictName());
            dataJson.put("data",entity);
            return JsonResult.ok("success",dataJson);
        }else {
            //查字典类型有没有
            DataDictType dataDictType = dictTypeService.selectByKey(dictType);
            if(dataDictType != null){
                entity.setId(dataDictType.getId());
                entity.setName(dataDictType.getTypeName());
                dataJson.put("data",entity);
            }
            return JsonResult.ok("success",dataJson);
        }
    }
}
